import Pyro.core
import Pyro.naming

class RMINameServer:
    def __init__(self):
        self.starter = Pyro.naming.NameServerStarter()

    def start(self):
        self.starter.start()

    def waitUntilStarted(self):
        self.starter.waitUntilStarted()


class RMIObject(Pyro.core.ObjBase):
    def __init__(self):
        Pyro.core.ObjBase.__init__(self)

    def register_service(self, service, service_url):
        self.getDaemon().connect(service, service_url)

class RMIServer():
    def __init__(self):
        Pyro.core.initServer()
        locator = Pyro.naming.NameServerLocator()
        ns = locator.getNS()
        self.daemon_ = Pyro.core.Daemon()
        self.daemon_.useNameServer(ns)

    def connect(self, service, service_name):
        self.daemon_.connect(service, service_name)

    def requestLoop(self):
        self.daemon_.requestLoop()

    def shutdown(self, force):
        self.daemon_.shutdown(force)

class RMIService():

    @staticmethod
    def service(service_name):
        service_url = "PYRONAME://{0}".format(service_name)
        return Pyro.core.getAttrProxyForURI(service_url)

    @staticmethod
    def register_service(service_name, service_instance):
        Pyro.core.initClient()
        locator = Pyro.naming.NameServerLocator()
        ns = locator.getNS()
        services = RMIService.service("services")
        services.connect(service_instance, service_name)    
        


def config():
    print "Starting Pyro..."
    Pyro.config.PYRO_TRACELEVEL = 3
    Pyro.config.PYRO_LOGFILE='log_file'
    Pyro.config.PYRO_MAXCONNECTIONS = 1000
